import { reqRegister } from "../../http/api"
import {
    Toast
} from "antd-mobile"

// 数据
const initState={
    // 1初始化数据
    user:{
        phone: "",
        password: "",
        nackname:""
    }

}


//action types
const types={
    //2 修改user type
    REGISTER_CHANGE_USER:"REGISTER_CHANGE_USER"
}


// actions creater
export const actions={
    // 4
    changeUser:(key,value)=>({
        type:types.REGISTER_CHANGE_USER,
        key,
        value
    }),
    // 5注册逻辑
    registerAction:push=>(dispatch,getState)=>{
        let {user}=getState().register
        reqRegister(user).then(res=>{
            if(res.data.code===200){
                Toast.info(res.data.msg, 1)
                push("/login")
            }
        })
    }
}



// reducer
export const registerReducer=(state=initState,action)=>{
    switch(action.type){
    // 3修改user
        case types.REGISTER_CHANGE_USER:
        return {
            ...state,
            user: {
                ...state.user,
                [action.key]: action.value
            }
        }
        default:
            return state
    }

}

// 导出数据 一堆函数
export const getUser = state => state.register.user;

